using Microsoft.AspNetCore.Mvc;

namespace TestStreamAPI.Controllers
{
    [ApiController]
    [Route("/Api/[controller]/[action]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries =
        [
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        ];

        [HttpPost]
        public async Task GetWeathersAsync()
        {
            Response.Headers.Add("Content-Type", "text/event-stream");
            Response.Headers.Add("Cache-Control", "no-cache");
            Response.Headers.Add("Connection", "keep-alive");
            for (int i = 0; i < 100; i++)
            {
                await Task.Delay(100);
                WeatherForecast weather = new()
                {
                    Date = new(2025, 4, 7),
                    TemperatureC = Random.Shared.Next(0, 100),
                    Summary = $"\n{Summaries[i % 5]}{i:00}"
                };
                await Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(weather));
                await Response.Body.FlushAsync();
            }
        }
    }
}
